#include "PAP_defs.h"
#include "derivative.h"
#include "GPS_Handler.h"

// MODIFY THE FOLLOWING PREPROCESSOR DIRECTIVES TO CONFIGURE SYSTEM OPTIONS

    //#define WATCHDOG_ENABLED
    //#define WATCHDOG_TIMEOUT_SHORT  
    //#define STOP_MODE_ENABLED 
    //#define BKGD_MODE_DISABLED
    //#define RESET_PIN_ENABLED
    
    //#define WATCHDOG_USE_BUS_CLOCK 
    //#define IIC_PIN_SELECT_ALTERNATE
    //#define ACOMP_INPUT_CAPTURE

// SYSTEM OPTION CONFIGURATION ENDS HERE

// MODIFY THE FOLLOWING PREPROCESSOR DIRECTIVES TO CONFIGURE COCK SOURCE

    /* Choose only one of the following 3 options */

    #define CLOCK_SOURCE_FLL_OUTPUT
    //#define CLOCK_SOURCE_INTERNAL
    //#define CLOCK_SOURCE_EXTERNAL

    /* clock source section ends */

    /* Choose powers of two, range: 1-128  */
    #define REFERENCE_DIVIDER 1     
    /* reference divider section ends   */

    #define REFERENCE_INTERNAL
    #define INTERNAL_REF_CLK_ENABLE
    #define INTERNAL_REF_STOP_ENABLE

    /* Choose powers of two, range: 1-8  */
    #define BUS_FREQ_DIVIDER 1
    /* bus frequency divider section ends  */

    //#define FREQ_RANGE_HIGH
    //#define HIGH_GAIN_OSC
    //#define LOW_POWER_SELECT
    //#define EXT_REF_REQ_OSCILLATOR
    //#define EXT_REF_ENABLED
    //#define EXT_REF_STOP_ENABLED

   

// CLOCK SOURCE CONFIGURATION ENDS HERE

// DO NOT MODIFY - INTEGRATED CLOCK SOURCE CONFIGURATION

#ifdef CLOCK_SOURCE_FLL_OUTPUT
#define CLK_SRC 0x00
#undef CLOCK_SOURCE_INTERNAL
#undef CLOCK_SOURCE_EXTERNAL
#endif

#ifdef CLOCK_SOURCE_INTERNAL
#define CLK_SRC 0x40
#undef CLOCK_SOURCE_EXTERNAL
#endif

#ifdef CLOCK_SOURCE_EXTERNAL
#define CLK_SRC 0x80
#endif

#if (REFERENCE_DIVIDER == 1)
#define REF_DIV 0x00
#elif (REFERENCE_DIVIDER == 2)
#define REF_DIV 0x08
#elif (REFERENCE_DIVIDER == 4)
#define REF_DIV 0x10
#elif (REFERENCE_DIVIDER == 8)
#define REF_DIV 0x18
#elif (REFERENCE_DIVIDER == 16)
#define REF_DIV 0x20
#elif (REFERENCE_DIVIDER == 32)
#define REF_DIV 0x28
#elif (REFERENCE_DIVIDER == 64)
#define REF_DIV 0x30
#elif (REFERENCE_DIVIDER == 128)
#define REF_DIV 0x38
#else  // if no valid divider is selected, system defaults to 1
#define REF_DIV 0x00
#endif

#ifdef REFERENCE_INTERNAL
#define REFERENCE 0x04
#else
#define REFERENCE 0x00
#endif

#ifdef INTERNAL_REF_CLK_ENABLE
#define INT_REF_CLK 0x02
#else
#define INT_REF_CLK 0x00
#endif

#ifdef INTERNAL_REF_STOP_ENABLE
#define INT_REF_STOP 0x01
#else
#define INT_REF_STOP 0x00
#endif

#define ICS_CONTROL_REG1 (CLK_SRC | REF_DIV | REFERENCE | INT_REF_CLK | INT_REF_STOP )

#if (BUS_FREQ_DIVIDER == 1)
#define BUS_DIV 0x00
#elif (BUS_FREQ_DIVIDER == 2)
#define BUS_DIV 0x40
#elif (BUS_FREQ_DIVIDER == 4)
#define BUS_DIV 0x80
#elif (BUS_FREQ_DIVIDER == 8)
#define BUS_DIV 0xC0
#else                           // if no valid bus divider is selected, 
#define BUS_DIV 0x40            // system defaults to 2
#endif

#ifdef FREQ_RANGE_HIGH
#define FREQ_RANGE 0x20
#else
#define FREQ_RANGE 0x00
#endif

#ifdef HIGH_GAIN_OSC
#define GAIN 0x01
#else
#define GAIN 0x00
#endif

#ifdef LOW_POWER_SELECT
#define LOW_POWER 0x80
#else
#define LOW_POWER 0x00
#endif

#ifdef EXT_REF_REQ_OSCILLATOR
#define EXT_OSCILLATOR 0x40
#else
#define EXT_OSCILLATOR 0x00
#endif

#ifdef EXT_REF_ENABLED
#define EXT_REF_ENABLE 0x02 
#else
#define EXT_REF_ENABLE 0x00
#endif

#ifdef EXT_REF_STOP_ENABLED
#define EXT_REF_STOP 0x01
#else
#define EXT_REF_STOP 0x00
#endif

#define ICS_CONTROL_REG2    (BUS_DIV | FREQ_RANGE | GAIN | LOW_POWER |      \
                            EXT_OSCILLATOR | EXT_REF_ENABLE | EXT_REF_STOP )

// DO NOT MODIFY SECTION (ICS CONFIGURATION) ENDS HERE

// DO NOT MODIFY - SOPT CONFIGURATION

#ifdef WATCHDOG_ENABLED
#define WATCHDOG_ENABLE 0x80
#else
#define WATCHDOG_ENABLE 0x00
#endif

#ifdef WATCHDOG_TIMEOUT_SHORT
#define WATCHDOG_TIMEOUT 0x00
#else
#define WATCHDOG_TIMEOUT 0x40
#endif

#ifdef STOP_MODE_ENABLED
#define STOP_MODE 0x20
#else
#define STOP_MODE 0x00
#endif

#ifdef BKGD_MODE_DISABLED
#define BKGD_MODE 0x00
#else
#define BKGD_MODE 0x02
#endif

#ifdef RESET_PIN_ENABLED
#define RESET_PIN 0x01
#else
#define RESET_PIN 0x00
#endif

#define SYS_CONFIG_1  (WATCHDOG_ENABLE | WATCHDOG_TIMEOUT | STOP_MODE |    \
                    BKGD_MODE | RESET_PIN)       

#ifdef WATCHDOG_USE_BUS_CLOCK
#define WATCHDOG_CLOCK 0x80
#else
#define WATCHDOG_CLOCK 0x00
#endif
                    
#ifdef IIC_PIN_SELECT_ALTERNATE
#define IIC_PIN_SELECT 0x02
#else
#define IIC_PIN_SELECT 0x00
#endif

#ifdef ACOMP_INPUT_CAPTURE
#define ACOMP_ICAPTURE 0x01
#else
#define ACOMP_ICAPTURE 0x00
#endif

#define SYS_CONFIG_2 (WATCHDOG_CLOCK | IIC_PIN_SELECT | ACOMP_ICAPTURE )


// DO NOT MODIFY ENDS (SOPT CONFIG) HERE


void vfnICS_Configure (void);

void vfnSystemOptionsInit (void);
